var fileauth = {};
//设置全局的分页
var page_ = new page({
	search : search
});

/**
 * 定义容器全局变量
 */
var con = new container({
	datagrid : '#datagrid',
	datagridtoolbar : '#datagrid-toolbar',
	singleSelect : true
}); 

function getCondition() {
	var condition = $("#listform").serializeJson();
	return condition;
}

$(document).ready(function() {
	/**
	 * 获取列表页面（行为）按钮
	 */
	var authCondition = {
			menuCode:curMenu.code,
			drawerCode:行为类型_前端组件,
			authAreaCode:授权分类_FTP管理_列表,
			callback:action,
			con:"#listform"}
	findResourcesByUserId(authCondition);
	/**
	 * 查询功能，因该页面已有查询权限，可直接硬编码调用
	 */
	search(page_);
});

function searchFtp(){
	search(page_);
}

/**
 * 查询
 * 
 * @param page
 */
function search(pg) {
	var url = ctx + "/ftp/query";
	page_.pageSize = pg.pageSize;
	page_.pageNumber = pg.pageNumber;
	query(url, getCondition(), con, page_);
}

function add() {
	$('#dialog').dialog(
			{
				title : '添加FTP配置',
				width : 450,
				height : 300,
				href : ctx + "/web/ftp/add.jsp",
				closed : false,
				cache : false,
				modal : true,
				onClose : function() {
					destroy('dialog', '#dialogDiv');
				},
				onLoad : function() {
					$("#addform").form('load', {});
				},
				buttons : [
						{
							text : '保存',
							iconCls : 'icon-ok',
							handler : function() {
								var bool = $("#addform").form(
										'enableValidation').form('validate');
								if (!bool) {
									return;
								}
								addFtp();
							},
						}, {
							text : '取消',
							iconCls : 'icon-cancel',
							handler : function() {
								$('#dialog').dialog('close');
							}
						} ]
			});
}
/**
 * 添加FTP
 */
function addFtp() {
	var data = $("#addform").serializeJson();
	data.organId = $("#index #organId").val();
	data.organName = $("#index #organName").val();
	data.organCode = $("#index #organCode").val();
	$.ajax({
		type : "POST",
		url : ctx + "/ftp/create",
		dataType : "json",
		data : data,
		success : function(data) {
			if(!showMsg(data,"添加成功")){
				return;
			}
			$('#dialog').dialog('close');
			search(page_);
		}
	});
}
/**
 * 修改FTP
 */
function update() {
	var row = getSelectedRow("#datagrid");
	if (!row) {
		return;
	}
	$('#dialog').dialog(
			{
				title : '修改',
				width : 450,
				height : 300,
				href : ctx + "/web/ftp/add.jsp",
				closed : false,
				cache : false,
				modal : true,
				onClose : function() {
					destroy('dialog', '#dialogDiv');
				},
				onLoad : function() {
					$("#addform").form('load', row);
					$("#rpw").textbox('setValue', $("#ftppassword").val());
				},
				buttons : [
						{
							text : '保存',
							iconCls : 'icon-ok',
							handler : function() {
								var bool = $("#addform").form(
										'enableValidation').form('validate');
								if (!bool) {
									return;
								}
								var data = $("#addform").serializeJson();
								data.id = row.id;
								data.organName = row.organName;
								data.organCode = row.organCode;
								data.organId = row.organId;
								$.ajax({
									type : "POST",
									url : ctx + "/ftp/update",
									dataType : "json",
									data : data,
									success : function(data) {
										if(!showMsg(data,"修改成功")){
											return;
										}
										$('#dialog').dialog('close');
										search(page_);
									}
								});
							}
						}, {
							text : '取消',
							iconCls : 'icon-cancel',
							handler : function() {
								$('#dialog').dialog('close');
							}
						} ]
			});
}
/**
 * 删除
 */
function del() {
	var row = getSelectedRow("#datagrid");
	if (!row) {
		message("请至少选择一行")
		return;
	}
	$.messager.confirm('提示', '确定要删除吗?', function(bool) {
		if (bool) {
			$.ajax({
				type : "get",
				url : ctx + "/ftp/delete",
				dataType : "json",
				data : {
					id : row.id
				},
				success : function(data) {
					if(!showMsg(data,"删除成功")){
						return;
					}
					search(page_);
				}
			});
		}
	});
}

/**
 * 测试
 */
function test() {
	var row = getSelectedRow("#datagrid");
	if (!row) {
		message("请至少选择一行")
		return;
	}
	$.ajax({
		type : "get",
		url : ctx + "/ftp/test",
		dataType : "json",
		data : {
			id : row.id
		},
		success : function(data) {
			if(!showMsg(data,"连接成功")){
				return;
			}
		}
	});
}

function uploadFtp() {
	var row = getSelectedRow("#datagrid");
	if (!row) {
		return;
	}
	upload("fid=" + row.id + "&note=我的");
}

/**
 * 详情
 */
function view() {
	var row = getSelectedRow("#datagrid");
	if (!row) {
		return;
	}
	$('#dialog').dialog(
			{
				title : '详情',
				width : 800,
				height : 450,
				href : ctx + '/web/ftp/view.jsp',
				closed : false,
				cache : false,
				onLoad : function() {
					$.each(row, function(key, value) {
						$("#addForm" + " span[id=" + key + "]").each(
								function(index, item) {
									if (value != null) {
										$(this).text(value);
									}
								});
					});
					
					
					
					/**
					 * 方式一开始
					 */
					files("#datagrid","#attachment");
					/**
					 * 方式一结束
					 */
					
					/**
					 * 方式二开始
					 */
					/**
					 * 获取详情页面（行为）按钮
					 */
					var authCondition = {
							menuCode:curMenu.code,
							drawerCode:行为类型_前端组件,
							authAreaCode:授权分类_FTP管理_详情,
							callback:action,
							con:"#datagrid-toolbar-download"}
					findResourcesByUserId(authCondition);
					files("#datagrid","#attachment1","#datagrid-toolbar-download");
					/**
					 * 方式二结束
					 */
				},
				modal : true,
				onClose : function() {
					destroy('dialog', '#dialogDiv');
				},
				buttons : [ {
					text : '返回',
					handler : function() {
						$('#dialog').dialog('close');
					}
				} ]
			});
}


function uploadGpy() {
	var id = 1;
	var url = ctx + '/web/upload/capture_.html?id='+id;
	window.open(url);
}

function webservice(){
	$.ajax({
		type : "get",
		url : ctx + "/cxf/wsTest",
		//url : ctx + "/cxf/restTest",
		dataType : "json",
		success : function(data) {
			if(!showMsg(data,"调用成功")){
				return;
			}
		}
	});
}

/**
 * 下载文件，由于downloadFile(row.id)为有参函数，需要封装一层
 */
function downloadFtpFile(){
	var row = getSelectedRow("#attachment1");
	if (!row) {
		return;
	}
	downloadFile(row.id);
}

/**
 * 删除文件，由于deleteFtpFile(row.id)为有参函数，需要封装一层
 */
function deleteFtpFile(){
	var row = getSelectedRow("#attachment1");
	if (!row) {
		return;
	}
	deleteFile(row.id,"#datagrid","#attachment1","#datagrid-toolbar-download");
}